﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;



namespace SportingGoodsExhibitionSystem
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            
        }
        
        string strCon = "server=.;database=CDB;user=sa;pwd=123456";

     

        public Boolean CheckUserExist(int stuNum, string passWord)
        {
            
            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from [student] where studentNumber=@un and password=@ps", con);
            cmd.Parameters.Add("@un", SqlDbType.Int, 10).Value = stuNum;
            cmd.Parameters.Add("@ps", SqlDbType.VarChar, 10).Value = passWord;
         
            SqlDataReader mysdr = cmd.ExecuteReader();
            if (mysdr.HasRows)
            {
                con.Close();
                return true;
            }
            else
            {
                con.Close();
                return false;
            }
        }
        public Boolean CheckUserExistAdmin(int adminNum, string passWord)
        {
            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from [admin] where adminNumber=@ad and passWord=@ps", con);
            cmd.Parameters.Add("@ad", SqlDbType.Int, 10).Value = adminNum;
            cmd.Parameters.Add("@ps", SqlDbType.VarChar, 10).Value = passWord;
            SqlDataAdapter read = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            read.SelectCommand.Connection = con; //
            read.Fill(dt);
            SqlDataReader mysdr = cmd.ExecuteReader();
            if (mysdr.HasRows)
            {
                con.Close();
                return true;
            }
            else
            {
                con.Close();
                return false;
            }
        }
        
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            
            
            if (comboBox.SelectedIndex==0)
            {
                try
                {
                    if (CheckUserExist(Convert.ToInt32(textBook1.Text), passWord.Password) == true)
                {
                       
                        MessageBox.Show("登陆成功");

                        int num = Convert.ToInt32(textBook1.Text);
                        SqlConnection con = new SqlConnection(strCon);
                        con.Open();
                        SqlCommand cmd = new SqlCommand("select * from [studentInfo] where studentNumber=@number  ", con);
                        cmd.Parameters.Add("@number", SqlDbType.Int, 10).Value = num;
                        SqlDataAdapter read = new SqlDataAdapter(cmd);
                        DataTable dt = new DataTable();
                        DataSet dataset = new DataSet();
                        read.Fill(dataset);
                        DataTable table = dataset.Tables[0];
                        DataRowCollection rows = table.Rows;
                        DataRow row = rows[0];
                        string name = row["studentName"].ToString();

                        MainMenu mm = new MainMenu(name, Convert.ToInt32(textBook1.Text));

                        int sta = 1;
                        cmd = new SqlCommand("update [studentInfo] set studentStatus=@num where studentNumber=@id", con);
                        cmd.Parameters.Add("@id", SqlDbType.VarChar, 50).Value = Convert.ToInt32(textBook1.Text);
                        cmd.Parameters.Add("@num", SqlDbType.VarChar, 50).Value = sta;
                        cmd.ExecuteNonQuery();
                        this.Close();
                    mm.Show();
                }
                else
                    MessageBox.Show("登录失败");
                }
                catch
                {
                    MessageBox.Show("输入有误");
                }
             
               
            }
            else
            {
                if (CheckUserExistAdmin(Convert.ToInt32(textBook1.Text), passWord.Password) == true)
                {
                    int num = Convert.ToInt32(textBook1.Text);
                    MessageBox.Show("登陆成功");
                    AdminMainMenu amm = new AdminMainMenu(textBook1.Text);
                    SqlConnection con = new SqlConnection(strCon);
                    con.Open();
                    SqlCommand cmd = new SqlCommand("update [admin] set status=@num where adminNumber=@id", con);
                    cmd.Parameters.Add("@id", SqlDbType.Int, 10).Value = Convert.ToInt32(textBook1.Text);
                    cmd.Parameters.Add("@num", SqlDbType.Int, 10).Value = 1;
                    cmd.ExecuteNonQuery();
                    con.Close();
                    this.Close();
                    amm.Show();
                }
                else
                    MessageBox.Show("登录失败");
            }
          
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            MainWindow mw = new MainWindow();
            mw.Hide();
            Regist regist = new Regist();
            this.Hide();
            regist.ShowDialog();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
        }
    }
}
